import face_recognition
import cv2
from imutils.video import VideoStream
from imutils.video import FPS
import imutils
import time

class camera:
    def __init__(self):
        self.result_face = ""
        # This is a demo of blurring faces in video.

        # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
        # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
        # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

        # initialize the video stream and allow the camera sensor to warm up
        print("[INFO] starting video stream…")
        self.vs = VideoStream(src=0).start()
        #vs = VideoStream(usePiCamera=True).start()
        time.sleep(2.0)


        # Initialize some variables
        self.face_locations = []
    
    # detect if people  in videos return null or loacation of array
    def detectPeople(self):        
        # Grab a single frame of video
        frame = self.vs.read()
        frame = imutils.resize(frame, width=500)
        # Resize frame of video to 1/4 size for faster face detection processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Find all the faces and face encodings in the current frame of video
        self.face_locations = face_recognition.face_locations(small_frame, model="cnn")


        # Display the results
        for top, right, bottom, left in self.face_locations:
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            # Extract the region of the image that contains the face
            face_image = frame[top:bottom, left:right]

            # Blur the face image
            face_image = cv2.GaussianBlur(face_image, (99, 99), 30)

            # Put the blurred face region back into the frame image
            frame[top:bottom, left:right] = face_image

        # Display the resulting image
        cv2.imshow('Video', frame)
        

        # Release handle to the webcam
        #self.vs.stop()
        #cv2.destroyAllWindows()
        self.result_face = self.face_locations
